package com.hnxxxy.controller;

import com.hnxxxy.pojo.Association;
import com.hnxxxy.service.AssociationAdministrationService;
import com.hnxxxy.utils.Communal;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/b/assAdm")
public class AssociationAdministrationController {
    private static final Logger log= LogManager.getLogger(AssociationAdministrationController.class);

    @Autowired
    AssociationAdministrationService associationAdministrationService;

    @Autowired
    Communal communal;

    /**
     * 查询本社团所有的人员信息
     * @return 本社团所有的人员信息
     */
    @RequestMapping(value="/f/assPeo",method= RequestMethod.POST)
    @ResponseBody
    public String findAllAssPeo(int page,int limit){
        log.debug("执行/f/assPeo接口");
        return associationAdministrationService.findAllAssPeo(page,limit);
    }

    /**
     * 查询本社团的人员信息
     * @param name 用户姓名
     * @return 查询本社团的人员信息
     */
    @RequestMapping(value="/s/assPeo",method= RequestMethod.POST)
    @ResponseBody
    public String queryAssPeo(@RequestParam("name") String name,int page,int limit){
        log.debug("执行/s/assPeo接口");
        if(name == null){return communal.resultMap(500, "请输入查询信息!");}
        return associationAdministrationService.queryAssPeo(name,page,limit);
    }

    /**
     * 删除本社团的人员
     * @param id user_ass表id
     * @return 操作成功或失败的信息
     */
    @RequestMapping(value="/d/assPeo",method= RequestMethod.POST)
    @ResponseBody
    public String deleteAssPeo(@RequestParam("id") int id){
        log.debug("执行/d/assPeo接口");
        if(id <= 0){return communal.resultMap(500, "请选择删除的用户!");}
        return associationAdministrationService.deleteAssPeo(id);
    }

    /**
     * 查询用户加入的社团信息
     * @return 用户加入的社团信息
     */
    @RequestMapping(value="/f/userAss",method= RequestMethod.POST)
    @ResponseBody
    public String findAllUserAss(int page,int limit){
        log.debug("执行/f/userAss接口");
        return  associationAdministrationService.findAllUserAss(page,limit);
    }

    /**
     * 退出社团
     * @param id user_ass表id
     * @return 操作成功或失败的信息
     */
    @RequestMapping(value="/d/userAss",method= RequestMethod.POST)
    @ResponseBody
    public String deleteUserAss(@RequestParam("id") int id){
        log.debug("执行/d/userAss接口");
        if(id <= 0){return communal.resultMap(500, "请选择退出的社团!");}
        return  associationAdministrationService.deleteAssPeo(id);
    }

    /**
     * 社团信息修改
     * @param map 社团p_id,社团名字a_name,社团社长username
     * @param img 社团图片
     * @return 操作成功或失败的信息
     */
    @RequestMapping(value = "/u/assInfo", method = RequestMethod.POST)
    @ResponseBody
    public String updateAssInfo(@RequestBody Map<String, String> map, @RequestParam("img") MultipartFile img, HttpServletRequest request) throws IOException {
        log.debug("执行/u/assInfo接口");
        String sjYz = communal.sjyz(map);
        if (sjYz != null) {
            return sjYz;
        }
        if(img == null){
            return communal.resultMap(500,"请选择图片");
        }
        String url = communal.path(img, request);
        String a_url = "";
        if(url != null){
            a_url = url;
        }else {
            return communal.resultMap(500,"修改图片失败");
        }
        Association association = new Association();
        association.Association1(Integer.parseInt(map.get("a_id")), map.get("a_name"), map.get("username"), a_url);
        return associationAdministrationService.updateAssInfo(association, request);
    }

    /**
     * 查询所有社团社长信息
     * @return 所有社团社长信息
     */
    @RequestMapping(value = "/f/assPre", method = RequestMethod.POST)
    @ResponseBody
    public String findAllAssPre(int page,int limit){
        log.debug("执行/f/assPre接口");
        return associationAdministrationService.findAllAssPre(page,limit);
    }

    /**
     * 条件查询社团社长信息
     * @param a_name 社团名字
     * @return 条件查询社团的社长信息
     */
    @RequestMapping(value = "/s/assPre", method = RequestMethod.POST)
    @ResponseBody
    public String queryAssPre( @RequestParam("a_name") String a_name,int page,int limit){
        log.debug("执行/s/assPre接口");
        if(a_name == null){return communal.resultMap(500, "请输入查询信息!");}
        return associationAdministrationService.queryAssPre(a_name,page,limit);
    }

    /**
     * 查询所有社团
     * @return 所有社团信息
     */
    @RequestMapping(value = "/f/assInfo", method = RequestMethod.POST)
    @ResponseBody
    public String findAllAssInfo(int page,int limit){
        log.debug("执行/f/assInfo接口");
        System.out.println(page);
        System.out.println(limit);
        return associationAdministrationService.findAllAssInfo(page,limit);
    }

    /**
     * 条件查询社团信息
     * @param a_name 社团名字
     * @return 条件查询的社团信息
     */
    @RequestMapping(value = "/s/assInfo", method = RequestMethod.POST)
    @ResponseBody
    public String queryAssInfo( @RequestParam("a_name") String a_name,int page,int limit){
        log.debug("执行/s/assInfo接口");
        if(a_name == null){return communal.resultMap(500, "请输入查询信息!");}
        return associationAdministrationService.queryAssInfo(a_name,page,limit);
    }

    /**
     * 删除社团
     * @param a_id 社团a_id
     * @return 操作成功或失败的信息
     */
    @RequestMapping(value="/d/assInfo",method= RequestMethod.POST)
    @ResponseBody
    public String deleteAssInfo(@RequestParam("a_id") int a_id){
        log.debug("执行/d/assInfo接口");
        if(a_id <= 0){return communal.resultMap(500, "请选择社团");}
        return  associationAdministrationService.deleteAssInfo(a_id);
    }
}
